package graphique;


import executer.Controleur;
import graphique.FormulaireCompte.ItemAction;
import graphique.FormulaireCompte.ItemState;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.*;
import javax.swing.text.MaskFormatter;


public class FormulaireContact extends JFrame{
    
    private JLabel nomLabel, prenomLabel, 
	annifLabel, rueLabel, villeLabel, paysLabel, telPortLabel, telFixeLabel, telBureauLabel, emailLabel, 
	arobLabel, pointLabel, obl;
	private JTextField nom, prenom, annif, rue, ville, pays,
		emailPart1, emailPart2, emailPart3;
	private JButton valide, annuler, cal;
	private JFormattedTextField telPort, telFixe, telBureau, dateAnnif;
	private CalDateN cdn = new CalDateN(this);
	private Controleur control;

	//variable servant a stocker les donnees entrees par l'utilisateur
	private String nomC = null, prenomC = null, dateC = null, telpC = null, telfC = null, telbC = null, 
	rueC = null, villeC = null, paysC = null, emailC = null;
    

	public FormulaireContact(String titre, Controleur control){
			super(titre);
			this.control = control;
			this.setSize(400, 500);
			this.setLocationRelativeTo(null);
			this.setResizable(false);
			//termine le processus lorsque l'on clic sur la croix
			this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			this.initContact();
			this.setVisible(true);
	}


    public void initContact(){
    	setLayout(new GridLayout(1, 6));
		
		//cadre identifiant
		JPanel panLog = new JPanel();
		
		panLog.setBackground(Color.white);
		panLog.setPreferredSize(new Dimension(300, 220)); //dimension du cadre
		
		panLog.setBorder(BorderFactory.createTitledBorder("Informations Personnelles"));
		
		//zone nom
		nom = new JTextField("");
		nom.setPreferredSize(new Dimension(130, 25));
		nomLabel = new JLabel("Nom* :");
		Box hBox7 = Box.createHorizontalBox();
        hBox7.add(nomLabel);
        hBox7.add(Box.createHorizontalStrut(70));
        hBox7.add(nom);
		
		//zone prenom
		prenom = new JTextField("");
		prenom.setPreferredSize(new Dimension(130, 25));
		prenomLabel = new JLabel("Prenom* :");
		Box hBox8 = Box.createHorizontalBox();
        hBox8.add(prenomLabel);
        hBox8.add(Box.createHorizontalStrut(50));
        hBox8.add(prenom);
		
		//zone adresse
        rueLabel = new JLabel("Rue :");
        rue = new JTextField("");
        rue.setPreferredSize(new Dimension(100, 25));
        Box hBox10 = Box.createHorizontalBox();
        hBox10.add(rueLabel);
        hBox10.add(Box.createHorizontalStrut(30));
        hBox10.add(rue); 
        
        villeLabel = new JLabel("Ville :");
        ville = new JTextField("");
        ville.setPreferredSize(new Dimension(100, 25));
        Box hBox11 = Box.createHorizontalBox();
        hBox11.add(villeLabel);
        hBox11.add(Box.createHorizontalStrut(75));
        hBox11.add(ville); 
        
        paysLabel = new JLabel("Pays :");
        pays = new JTextField("");
        pays.setPreferredSize(new Dimension(100, 25));
        Box hBox12 = Box.createHorizontalBox();
        hBox12.add(paysLabel);
        hBox12.add(Box.createHorizontalStrut(70));
        hBox12.add(pays); 
		
		//zone date de naissance
		annifLabel = new JLabel("Date de naissance :");
		
		cal = new JButton("annee de naissance");
		cal.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				cdn.setVisible(true);
				
				
			}
		});
		
		dateAnnif = new JFormattedTextField(DateFormat.getDateInstance());
		dateAnnif.setPreferredSize(new Dimension(100, 25));
		try{
        	   MaskFormatter dateA = new MaskFormatter("##-##-####");
        	   dateAnnif = new JFormattedTextField(dateA);
        }catch(ParseException e){
        	e.printStackTrace();
        }
		
		Box hBox9 = Box.createHorizontalBox();
        hBox9.add(annifLabel);
        hBox9.add(Box.createHorizontalStrut(5));
        hBox9.add(cal);
		
		Box vBox3 = Box.createVerticalBox();
        vBox3.add(hBox7);
        vBox3.add(Box.createVerticalStrut(5));
        vBox3.add(hBox8);
        vBox3.add(Box.createVerticalStrut(5));
        vBox3.add(hBox10);
        vBox3.add(Box.createVerticalStrut(5));
        vBox3.add(hBox11);
        vBox3.add(Box.createVerticalStrut(5));
        vBox3.add(hBox12);
        vBox3.add(Box.createVerticalStrut(5));
        vBox3.add(hBox9);
        panLog.add(vBox3);
		
		//cadre contact
		JPanel panCon = new JPanel();
		
		panCon.setBackground(Color.white);
		panCon.setPreferredSize(new Dimension(300, 160)); //dimension du cadre
		panCon.setBorder(BorderFactory.createTitledBorder("Contacts"));
				
		telPort = new JFormattedTextField(NumberFormat.getIntegerInstance());
		telFixe = new JFormattedTextField(NumberFormat.getIntegerInstance());
		telBureau = new JFormattedTextField(NumberFormat.getIntegerInstance());
		try{
        	   MaskFormatter telP = new MaskFormatter("##-##-##-##-##");
        	   MaskFormatter telF = new MaskFormatter("##-##-##-##-##");
        	   MaskFormatter telB = new MaskFormatter("##-##-##-##-##");
        	   telPort = new JFormattedTextField(telP);
        	   telFixe = new JFormattedTextField(telF);
        	   telBureau = new JFormattedTextField(telB);
        }catch(ParseException e){
        	e.printStackTrace();
        }

		telPort.setPreferredSize(new Dimension(100, 25));
		telPortLabel = new JLabel("Telephone mobile :");
		Box hBox1 = Box.createHorizontalBox();
        hBox1.add(telPortLabel);
        hBox1.add(Box.createHorizontalStrut(15));
        hBox1.add(telPort);

		telFixe.setPreferredSize(new Dimension(100, 25));
		telFixeLabel = new JLabel("Telephone fixe :");
		Box hBox3 = Box.createHorizontalBox();
        hBox3.add(telFixeLabel);
        hBox3.add(Box.createHorizontalStrut(30));
        hBox3.add(telFixe);
        
		telBureau.setPreferredSize(new Dimension(100, 25));
		telBureauLabel = new JLabel("Telephone bureau :");
		Box hBox4 = Box.createHorizontalBox();
        hBox4.add(telBureauLabel);
        hBox4.add(Box.createHorizontalStrut(15));
        hBox4.add(telBureau);
		
		emailLabel = new JLabel("Email :");
		arobLabel = new JLabel("@");
		pointLabel = new JLabel(".");
		emailPart1 = new JTextField("");
		emailPart1.setPreferredSize(new Dimension(80, 25));
		emailPart2 = new JTextField("");
		emailPart2.setPreferredSize(new Dimension(40, 25));
		emailPart3 = new JTextField("");
		emailPart3.setPreferredSize(new Dimension(20, 25));
		Box hBox2 = Box.createHorizontalBox();
        hBox2.add(emailLabel);
        hBox2.add(Box.createHorizontalStrut(2));
        hBox2.add(emailPart1);
        hBox2.add(Box.createHorizontalStrut(2));
        hBox2.add(arobLabel);
        hBox2.add(Box.createHorizontalStrut(2));
        hBox2.add(emailPart2);
        hBox2.add(Box.createHorizontalStrut(2));
        hBox2.add(pointLabel);
        hBox2.add(Box.createHorizontalStrut(2));
        hBox2.add(emailPart3);
        
        Box vBox = Box.createVerticalBox();
        vBox.add(hBox1);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox3);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox4);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox2);
        panCon.add(vBox);
		
        valide = new JButton("Creer Contact");
        valide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nomC = nom.getText();
				prenomC = prenom.getText();
				dateC = dateAnnif.getText();
				rueC = rue.getText();
				villeC = ville.getText();
				paysC = pays.getText();
				telpC = telPort.getText();
				telfC = telFixe.getText();
				telbC = telBureau.getText();
				emailC = emailPart1.getText() + "@" + emailPart2.getText() + "." + emailPart3.getText();
				System.out.println(nomC + " " + prenomC + " " + dateC + " " + rueC + " " + emailC);
                String[] data = new String[10];
                data[0] = nomC;
                data[1] = prenomC;
                data[2] = rueC;
                data[3] = villeC;
                data[4] = paysC;
                data[5] = telpC;
                data[6] = telfC;
                data[7] = emailC;
                data[8] = dateC;
				if(nomC.isEmpty() || prenomC.isEmpty()){
					boitePwdErreur();
				}
				else {
					boiteSucces();
					dispose();
					control.ajouterContact(data);
				}
					
			}
		});
        
        annuler = new JButton("Annuler");
        annuler.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent event){
    			dispose();
    		}
    	});
        
        obl = new JLabel("* champs obligatoire");
        obl.setFont(new Font("ARIAL",Font.ITALIC,10));
		
		JPanel content1 = new JPanel();
		content1.setBackground(Color.white);
		//JPanel content2 = new JPanel();
		content1.add(panLog);
		content1.add(panCon);
		content1.add(valide);
		content1.add(annuler);
		content1.add(obl);
		add(content1, BorderLayout.NORTH);
		//add(content2, BorderLayout.SOUTH);
    }
    
    private void boitePwdErreur()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Erreur dans la saisie du contact",
                                      "Erreur", 
                                      JOptionPane.ERROR_MESSAGE);
    }
    
    private void boiteSucces()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Ajout du contact reussi",
                                      "Information", 
                                      JOptionPane.INFORMATION_MESSAGE);
    }

    public String getNomContact(){
    	return nomC;
    }
    
    public String getPrenomContact(){
    	return prenomC;
    }
    
    public String getDateContact(){
    	return dateC;
    }
    
    public void setDateContact(String _date){
    	dateC = _date;
    }
    
    public String getRueContact(){
    	return rueC;
    }
    
    public String getVilleContact(){
    	return villeC;
    }
    
    public String getPaysContact(){
    	return paysC;
    }
    
    public String getTelPContact(){
    	return telpC;
    }
    
    public String getTelFContact(){
    	return telfC;
    }
    
    public String getTelBContact(){
    	return telbC;
    }
    
    public String getEmailContact(){
    	return emailC;
    }
    
}
